package pl.wroc.uni.ii.evolution.distribution.statistics.applet;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import pl.wroc.uni.ii.evolution.servlets.databasesupport.communication.EvDBServletCommunication;

/**
 * Component on which you can select domain of the statistics
 * (nodes, cells)
 * 
 * @author Kacper Gorski
 *
 */
public class EvDistStatAppletDomainSelector extends JPanel implements ActionListener {
  
  private static final long serialVersionUID = 1L;  
  
  private JLabel title = new JLabel("Select statistics domain:");
  private JLabel title2 = new JLabel("Cell:");
  private JLabel title3 = new JLabel("Node:");
  private JComboBox nodes_list = new JComboBox();
  private JComboBox cells_list = new JComboBox();
  private EvDBServletCommunication db_comm;
  private Integer task_id = null;
  
  public EvDistStatAppletDomainSelector(EvDBServletCommunication db_comm) {
    
    this.db_comm = db_comm;
    setLayout(null);
    
    title.setBounds(0 ,0 , 150, 20);
    add(title);    
    
    title2.setBounds(0, 22, 150, 20);
    add(title2);
    cells_list.setBounds(0 ,40 , 150, 20);
    add(cells_list);
    cells_list.addActionListener(this);
    
    title3.setBounds(200, 22, 150, 20);
    add(title3);
    nodes_list.setBounds(200 ,40 , 150, 20);
    add(nodes_list); 
    
  }


  /**
   * Updates subcomponents and fills them with nodes, cells ids 
   * 
   * @param db - database
   * @param task_id
   */
  public void refresh(int task_id) {
    try {
     
      this.task_id = task_id;
     // removing old list of nodes und adding a new on
    
      Long[] cells = db_comm.getCellIdsWithStatistics(task_id);
      
      // removing old list of cells und adding a new one
      cells_list.removeAllItems();
      
      if (cells != null)  
      {
        for (int i=0; i < cells.length; i++) {
          cells_list.addItem(cells[i]+"");
        }
      }
       
    } catch (IOException e) {
      e.printStackTrace();
    }
    
  }


  public void actionPerformed(ActionEvent arg0) {
        

    Long cell = getCellId();
    
    if (task_id != null && cell != null) {
    
      Long[] nodes = null;
      try {
        nodes = db_comm.getNodesIdsWithStatistics(task_id, cell);
      } catch (IOException e) {
        e.printStackTrace();
      }

      nodes_list.removeAllItems();
      if (nodes != null)
      {
      
        for (int j=0; j < nodes.length; j++) {
          nodes_list.addItem(nodes[j]+"");
        }
      }

    }
    
    
  }


  public Long getCellId() {
    Object[] objects = cells_list.getSelectedObjects();
    if (objects != null && objects.length > 0) {
      return Long.parseLong((String)objects[0]);
    }
    
    return null;
    
  }


  public Long getNodeId() {
    Object[] objects = nodes_list.getSelectedObjects();
    if (objects != null && objects.length > 0) {
      return Long.parseLong((String)objects[0]);
    }
    return null;
  }
  
}
